//
//  BRMContactView.m
//  BrezzeSaaS
//
//  Created by 甘卫国 on 2024/1/11.
//  Copyright © 2024 Brezze GWG. All rights reserved.
//

#import "BRMContactView.h"
#import "BRMInfoListView.h"

@interface BRMContactView  ()
{
    UIView *_botomView;
}
@end

@implementation BRMContactView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _canEdit = YES;
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 13;
        self.layer.masksToBounds = YES;
        [self setUI];
    }
    return self;
}

- (void)br_textChange:(NSString *)title text:(NSString *)text by:(NSInteger)index {
    
    
    if (self.contactTextChangBlock) {
        self.contactTextChangBlock(@{@"title":title,@"text":text,@"index":@(index)});
    }

}

- (void)br_setOtp:(NSInteger)code by:(NSInteger)index {
    
    BRMInfoListMoreView *cell = [self viewWithTag:index + 500];
    cell.otpView.codeL.text = [NSString stringWithFormat:@"%ld",code];
}

- (void)br_text:(NSString *)text by:(NSInteger)index {
    
    [self br_title:@"" text:text by:index];
}

- (void)br_setLineHidden:(BOOL)hidden by:(NSInteger)index {
    
    BRMInfoListMoreView *cell = [self viewWithTag:index + 500];
    cell.lineHidde = hidden;
}

- (void)br_title:(NSString *)title text:(NSString *)text by:(NSInteger)index {
    
    BRMInfoListMoreView *cell = [self viewWithTag:index + 500];
    
    if (text.length != 0) {
        cell.textView.text = text;
    }
    if (title.length != 0) {
        cell.titleStr = title;
    }
}


- (void)setHasAccount:(BOOL)hasAccount {
    _hasAccount = hasAccount;
    _botomView.hidden = hasAccount;
}

- (void)setIsAdd:(BOOL)isAdd {
    _isAdd = isAdd;
    
    [self br_setLineHidden:isAdd by:1];
    
    _botomView.hidden = isAdd;
}

//- (void)setDto:(BRMAgentsEditModel *)dto {
//    _dto = dto;
//    
//    [self br_text:dto.principal by:0];
//    [self br_text:dto.phone by:1];
//    [self br_setOtp:dto.code.integerValue by:1];
//    [self br_text:dto.account by:2];
//}



#pragma mark - UI --
- (void)setUI {
    
    self.backgroundColor = UIColor.qd_backgroundColor2;
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.spacing = 0;
    stack.axis = UILayoutConstraintAxisVertical;
    [self addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
//        make.bottom.offset(-140);
        make.bottom.offset(0);
        make.top.offset(46);
    }];
    
    UIView *contView = [[UIView alloc] init];
    UIView *botomView = [[UIView alloc] init];
    _botomView = botomView;
    [stack addArrangedSubview:contView];
    [stack addArrangedSubview:botomView];
    
    self.titleL = [BRToolsView br_label:@"--" textColor:UIColor.qd_titleTextColor font:[UIFont fontS:17]];
    [self addSubview:self.titleL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.bottom.equalTo(contView.mas_top).offset(-8);
    }];
    
    NSArray *arr = @[Localized(@"shop_inCharge"),Localized(@"shop_contactNum"),Localized(@"shop_ownerAount")];
    
    UIView *view;
    for (int i = 0; i < arr.count; i++) {
        BRMInfoListMoreView *cell = [[BRMInfoListMoreView alloc] init];
        cell.placeholder = Localized(@"shop_pleaseEnter");
        NSString *title = arr[i];
        cell.titleStr = title;
        cell.tag = 500 +i;
        
        if ([title isEqualToString:Localized(@"shop_contactNum")]) {
            [cell br_showOtpView];
            cell.listType = BRMShopInf_phone;
            cell.textView.maximumTextLength = 15;
        }
        cell.userInteractionEnabled = ![title isEqualToString:Localized(@"shop_ownerAount")];
        [contView addSubview:cell];
        [cell mas_makeConstraints:^(MASConstraintMaker *make) {
            if (!view) {
                make.top.offset(0);
            }else {
                make.top.equalTo(view.mas_bottom).offset(0);
            }
            make.left.right.offset(0);
            
            if (i == arr.count -1) {
                make.bottom.offset(0);
            }
        }];
        cell.lineHidde = (i == arr.count -1);
        view = cell;
        WS(weakSelf);
        cell.textChangBlock = ^(NSString * _Nonnull text) {
            [weakSelf br_textChange:title text:text by:i];
        };
        
        cell.toOtpBlock = ^{
            if (weakSelf.toOtpBlock) weakSelf.toOtpBlock();
        };
    }
    

}

@end
